package tdt4240.iddqd;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.menu.MenuScene;
import org.anddev.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.anddev.andengine.entity.scene.menu.item.IMenuItem;
import org.anddev.andengine.extension.input.touch.controller.MultiTouchController;
import org.anddev.andengine.extension.input.touch.exception.MultiTouchException;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import tdt4240.iddqd.camera.GameCamera;
import tdt4240.iddqd.database.DatabaseAdapter;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.DisplayMetrics;
import android.view.KeyEvent;

public class GameActivity extends BaseGameActivity implements IOnMenuItemClickListener {

	public static int CAMERA_WIDTH;
	public static int CAMERA_HEIGHT;

	public static GameActivity activity;

	private GameCamera mCamera;
	private GameScene mGameScene;
	private GameMenuScene mMenuScene;
	private GameMenuScene mGameOverScene;
	private GameMenuScene mGameWonScene;

	public static MapFactory mMapFactory;
	public static DatabaseAdapter mDatabaseAdapter;

	public static BitmapTextureAtlas mGrassBitmapTextureAtlas;
	public static BitmapTextureAtlas mHedgeTextureAtlas;
	public static BitmapTextureAtlas mPlantTextureAtlas;
	public static BitmapTextureAtlas mFlowerTextureAtlas;
	public static BitmapTextureAtlas mBlankTextureAtlas;

	public static TiledTextureRegion mPlayerTexture;
	public static TextureRegion mGrassBackground;
	public static TiledTextureRegion mHedge;
	public static TiledTextureRegion mPlant;
	public static TiledTextureRegion mFlower;
	public static TiledTextureRegion mBlank;

	private BitmapTextureAtlas mPlayerStandStillTextureAtlas;
	private BitmapTextureAtlas mPlayerWalkTextureAtlas;
	private BitmapTextureAtlas mSwordTextureAtlas;
	private BitmapTextureAtlas mEnemyWalkTextureAtlas;
	public static TiledTextureRegion mPlayerStandStill;
	public static TiledTextureRegion mPlayerWalk;
	public static TiledTextureRegion mEnemyWalk;
	public static TiledTextureRegion mSword;
	public static BitmapTextureAtlas mOnScreenControlTextureAtlas;
	public static TextureRegion mOnScreenControlBaseTextureRegion;
	public static TextureRegion mOnScreenControlKnobTextureRegion;

	public static BitmapTextureAtlas mMenuContinueTextureAtlas;
	public static BitmapTextureAtlas mMenuResetTextureAtlas;
	public static BitmapTextureAtlas mMenuQuitTextureAtlas;
	public static TextureRegion mMenuContinueTextureRegion;
	public static TextureRegion mMenuResetTextureRegion;
	public static TextureRegion mMenuQuitTextureRegion;

	public static BitmapTextureAtlas mFontTextureAtlas;
	public static BitmapTextureAtlas mFontTexture2Atlas;
	public static Font mFont;
	public static Font mGameOverFont;


	@Override
	public Engine onLoadEngine() {

		DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);

		CAMERA_HEIGHT = metrics.heightPixels;
		CAMERA_WIDTH = metrics.widthPixels;

		activity = this;

		mCamera = new GameCamera();

		Engine mEngine = new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), mCamera));
		try {
			mEngine.setTouchController(new MultiTouchController());
		} catch (MultiTouchException e) {
			e.printStackTrace();
		}
		return mEngine;
	}

	@Override
	public void onLoadResources() {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		mPlayerStandStillTextureAtlas = new BitmapTextureAtlas(16, 16, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		mPlayerStandStill = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mPlayerStandStillTextureAtlas, this, "stand.png", 0, 0, 1, 1);

		mPlayerWalkTextureAtlas = new BitmapTextureAtlas(128, 16, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		mPlayerWalk = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mPlayerWalkTextureAtlas, this, "walk.png", 0, 0, 8, 1);

		mEnemyWalkTextureAtlas = new BitmapTextureAtlas(128, 16, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		mEnemyWalk = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mEnemyWalkTextureAtlas, this, "walk_enemy.png", 0, 0, 8, 1);

		mGrassBitmapTextureAtlas = new BitmapTextureAtlas(64,	64, TextureOptions.REPEATING_NEAREST_PREMULTIPLYALPHA);
		mGrassBackground = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mGrassBitmapTextureAtlas, this, "grass.png", 0,0);
		mGrassBackground.setWidth(200*mGrassBackground.getWidth());
		mGrassBackground.setHeight(200*mGrassBackground.getHeight());

		mHedgeTextureAtlas = new BitmapTextureAtlas(64,	32, TextureOptions.REPEATING_NEAREST_PREMULTIPLYALPHA);
		mHedge = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mHedgeTextureAtlas, this, "wall.png", 0, 0, 1, 1);

		mPlantTextureAtlas = new BitmapTextureAtlas(16,	16, TextureOptions.REPEATING_NEAREST_PREMULTIPLYALPHA);
		mPlant = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mPlantTextureAtlas, this, "plant.png", 0, 0, 1, 1);

		mFlowerTextureAtlas = new BitmapTextureAtlas(16,	16, TextureOptions.REPEATING_NEAREST_PREMULTIPLYALPHA);
		mFlower = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mFlowerTextureAtlas, this, "flower.png", 0, 0, 1, 1);

		mSwordTextureAtlas = new BitmapTextureAtlas(128, 16, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		mSword = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mSwordTextureAtlas, this, "sword.png", 0, 0, 4, 1);

		mBlankTextureAtlas = new BitmapTextureAtlas(1, 1, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		mBlank = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mBlankTextureAtlas, this, "blank.png", 0, 0, 1, 1);

		mOnScreenControlTextureAtlas = new BitmapTextureAtlas(256, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		mOnScreenControlBaseTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mOnScreenControlTextureAtlas, this, "base.png", 0, 0);
		mOnScreenControlKnobTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mOnScreenControlTextureAtlas, this, "knob.png", 128, 0);

		mMenuContinueTextureAtlas = new BitmapTextureAtlas(256, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		mMenuContinueTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mMenuContinueTextureAtlas, this, "menu_continue.png", 0, 0);

		mMenuResetTextureAtlas = new BitmapTextureAtlas(256, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		mMenuResetTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mMenuResetTextureAtlas, this, "menu_reset.png", 0, 0);

		mMenuQuitTextureAtlas = new BitmapTextureAtlas(256, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		mMenuQuitTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mMenuQuitTextureAtlas, this, "menu_quit.png", 0, 0);
		mEngine.getTextureManager().loadTextures(mMenuContinueTextureAtlas, mMenuResetTextureAtlas, mMenuQuitTextureAtlas);		

		//Load font
		mFontTextureAtlas = new BitmapTextureAtlas(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		mFontTexture2Atlas = new BitmapTextureAtlas(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		mFont = new Font(mFontTextureAtlas, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 32, true, Color.BLACK);
		mGameOverFont = new Font(mFontTexture2Atlas, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 60, true, Color.RED);
		mEngine.getTextureManager().loadTextures(mPlayerStandStillTextureAtlas, mPlayerWalkTextureAtlas, mEnemyWalkTextureAtlas, mHedgeTextureAtlas, mOnScreenControlTextureAtlas, mFontTextureAtlas, mFontTexture2Atlas, mGrassBitmapTextureAtlas, mPlantTextureAtlas, mFlowerTextureAtlas);
		mEngine.getFontManager().loadFont(mFont);
		mEngine.getFontManager().loadFont(mGameOverFont);

	}

	@Override
	public Scene onLoadScene() {
		mDatabaseAdapter = new DatabaseAdapter(this);
		mMapFactory = new FileMapFactory(getAssets());
		
		if(getIntent().getIntExtra(Constants.GAME_TYPE, 1) == Constants.GAME_CONTINUE) {
			int[] save = mDatabaseAdapter.load();
			mMapFactory.currentLevelId = save[0];
			mMapFactory.currentCheckpointId = save[1];
		}
		
		mGameScene = new GameScene(mCamera);

		mMenuScene = new GameMenuScene(mCamera);
		mMenuScene.setOnMenuItemClickListener(this);

		mGameOverScene = new GameMenuScene(mCamera, getString(R.string.youDied));
		mGameOverScene.setOnMenuItemClickListener(this);

		mGameWonScene = new GameMenuScene(mCamera, getString(R.string.youWon));
		mGameWonScene.setOnMenuItemClickListener(this);

		return mGameScene;
	}

	@Override
	public void onLoadComplete() {
	}

	@Override
	public boolean onKeyDown(final int pKeyCode, final KeyEvent pEvent) {

		if(pKeyCode == KeyEvent.KEYCODE_MENU && pEvent.getAction() == KeyEvent.ACTION_DOWN) {			
			mGameScene.setChildScene(mMenuScene, false, true, true);
			return true;
		} else {
			return super.onKeyDown(pKeyCode, pEvent);
		}
	}

	@Override
	public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem,
			float pMenuItemLocalX, float pMenuItemLocalY) {
		switch(pMenuItem.getID()) {
		case Constants.MENU_CONTINUE:
			/* Return to game. */
			mMenuScene.back();
			return true;
		case Constants.MENU_RESET:
			/* Restart the animation. */
			mMenuScene.reset();
			mGameOverScene.reset();
			mGameWonScene.reset();
			mMenuScene.reset();
			return true;
		case Constants.MENU_QUIT:
			/* End Activity. */
			finish();
			return true;
		default:
			return false;

		}
	}

	/**
	 * If the player completed all levels of the game
	 */
	public void playerWon() {
		mGameScene.setChildScene(mGameWonScene, false, true, true);
	}

	/**
	 * If the player died during the game
	 */
	public void playerIsDead(){
		mGameScene.setChildScene(mGameOverScene, false, true, true);
	}

}
